<template>
	<view class="list-view">
		<view class="top-bar"><uni-navbar :fixed="true" :back="true" color="#fff" title="网上信访"></uni-navbar></view>

		<view class="search-view"><u-search class="search-input" shape="square" bg-color="#fff" @search="search" @custom="search"></u-search></view>

		<u-tabs class="btm-tabs" :list="tabsList" :current="tabsCurrent" @change="tabsChange" :is-scroll="false"></u-tabs>

		<view class="list-box" :style="{ background: loading == 'nodata' ? 'none' : '#fff' }">
			<view class="list-item" v-for="(item, index) of infoList" :key="item.id">
				<view class="list-info_02" @click="navTo('/pagesB/petition/detail?id=' + item.id)">
					<view class="left">
						<view class="info-title">{{ item.title }}</view>
						<view class="info-btm">
							<view class="name clamp">{{ item.submitBy }}</view>
							<text class="line"></text>
							<view class="time">{{ item.createTime | parseTime('{y}-{m}-{d}') }}</view>
						</view>
					</view>
					<view class="right" :style="'color:' + colorMap[item.nodeStatus]">{{ item.nodeStatusValue }}</view>
				</view>
				<view class="info-btn" v-if="item.nodeStatus == '0'"><u-button class="sub-btn" type="error" plain size="medium" shape="circle" @click="edit(item)">回复</u-button></view>
			</view>
			<u-loadmore :status="loading" />
		</view>

		<view class="btn-add" @click="navTo('/pagesB/petition/add')"><u-icon name="plus" color="#fff" size="38"></u-icon></view>

		<u-back-top :scroll-top="scrollTop" top="200"></u-back-top>
	</view>
</template>

<script>
import { mapState } from 'vuex';
export default {
	data() {
		return {
			scrollTop: 0,
			tabsCurrent: 0,
			tabsList: [
				{
					name: '全部',
					value: ''
				},
				{
					name: '未回复',
					value: '0'
				},
				{
					name: '已回复',
					value: '1'
				}
			],
			colorMap: {
				0: '#ff9900',
				1: '#19be6b',
				2: '#0099ff',
				3: '#e5231a'
			},
			title: '',
			infoList: [],

			param: {
				isRead: '',
				nodeStatus: '',
				title: ''
			},
			loading: 'loadmore'
		};
	},
	onLoad() {
		this.loadLogin();
		this.loadData();
	},
	onPullDownRefresh() {
		this.loadData('refresh');
	},
	onReachBottom() {
		if (this.loading == 'loadmore') this.loadData('add');
	},
	onPageScroll(res) {
		this.scrollTop = res.scrollTop;
	},
	computed: {
		...mapState(['userInfo'])
	},
	methods: {
		loadLogin() {
			if (this.userInfo.Authorization) {
				this.clientCode = this.userInfo.sysUser.clientCode;
				this.userId = this.userInfo.sysUser.id;

				this.pageNum = 1;
				this.pageSize = 10;
			} else {
				this.$logout();
			}
		},
		loadData(type = 'add') {
			if (type == 'add' && this.loading == 'loading') {
				return false;
			}
			if (type == 'add' && this.loading == 'nomore') {
				return false;
			}

			this.loading = 'loading';
			if (type != 'add') {
				this.pageNum = 1;
			}
			let url = '/wangshangxinfang/petition-letter/list';
			let data = {
				pageNum: this.pageNum,
				pageSize: this.pageSize,
				clientCode: this.clientCode,
				userId: this.userId,
				...this.param
			};

			this.$request(url, data, {
				load: true
			}).then((res) => {
				let count = 0;
				if (type != 'add') {
					uni.stopPullDownRefresh();
					this.infoList = [];
				}
				this.loading = this.pageNum < Math.ceil(res.data.total / this.pageSize) ? 'loadmore' : 'nomore';
				this.pageNum = this.pageNum + 1;
				this.infoList = this.infoList.concat(res.data.records);
				this.loaded = true;
				if (this.infoList.length <= 0) {
					this.loading = 'nodata';
				}
			});
		},
		tabsChange(index) {
			this.tabsCurrent = index;
			this.typeHandle(this.tabsList[index].value);
		},
		search(e) {
			this.param.title = e;
			this.loadData('refresh');
		},
		typeHandle(e) {
			this.param.nodeStatus = e || '';
			this.loadData('refresh');
		},
		edit(item) {
			uni.navigateTo({
				url: '/pagesB/petition/detail?type=reply&id=' + item.id
			});
		},
		navTo(url) {
			uni.navigateTo({
				url: url
			});
		}
	}
};
</script>

<style lang="scss">
.list-box {
}

.info-btn {
	display: flex;
	justify-content: flex-end;
	align-items: center;
	padding: 20rpx 0;
	width: 100%;
	border-bottom: 1rpx solid $u-border-color;

	.sub-btn {
		margin: 0 0 0 20rpx;

		&:first-child {
			margin: 0;
		}
	}
}

.list-item {
	padding: 0 30rpx;
	border-bottom: 20rpx solid $u-border-color;
}
</style>
